package com.zz.parkingsystem.controller.admin;

import com.zz.parkingsystem.dto.PdaDTO;
import com.zz.parkingsystem.entity.Pda;
import com.zz.parkingsystem.service.PCPdaService;
import com.zz.parkingsystem.util.Result;
import com.zz.parkingsystem.util.ResultGenerator;
import com.zz.parkingsystem.vo.AllPdaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "pda管理")
@RequestMapping("admin/pda")
public class AdminPdaController {
    @Resource
    private PCPdaService pcPdaService;

    @ApiOperation(value = "所有pda设备展示",notes = "可以根据pda名称，pda编号，路段编号进行查询")
    @PostMapping("/getAllPda")
    public Result getAllPda(@RequestBody PdaDTO pdaDto){
        pdaDto.setStartPage((pdaDto.getCurrentPage()-1)*pdaDto.getPageSize());
        List<AllPdaVO> list = pcPdaService.getAllPda(pdaDto);
        Long total = pcPdaService.getCount(pdaDto);
        System.out.println(total);
        Map map = new HashMap();
        map.put("list",list);
        map.put("total",total);
        return ResultGenerator.genSuccessResult(map);
    }

    @ApiOperation(value = "查询单个设备信息",notes = "根据id查询对应pda信息")
    @GetMapping("{id}")
    public Result getPdaOne(@PathVariable("id") Long id){
        Pda pda = pcPdaService.selectByPrimaryKey(id);
        return ResultGenerator.genSuccessResult(pda);
    }

    @ApiOperation(value = "增加设备",notes = "添加pda设备")
    @PostMapping("")
    public Result addPda(@RequestBody Pda pda){
        int i = pcPdaService.insertSelective(pda);
        return ResultGenerator.genSuccessResult("添加成功");
    }

    @ApiOperation(value = "修改地磁信息",notes = "根据id修改pda信息")
    @PutMapping("{id}")
    public Result update(@RequestBody Pda pda){
        int i = pcPdaService.updateByPrimaryKeySelective(pda);
        return ResultGenerator.genSuccessResult("修改信息成功");
    }

    @ApiOperation(value = "删除地磁信息",notes = "根据id删除地磁信息")
    @DeleteMapping("{id}")
    public Result delPda(@PathVariable("id") Long id){
        int i = pcPdaService.deleteByPrimaryKey(id);
        return ResultGenerator.genSuccessResult("删除信息成功");
    }
}
